package com.appspot.niehonglei.action;

import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.niehonglei.dao.PMF;
import com.appspot.niehonglei.jdo.GuestBook;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class SaveGuestBook implements ActionInterface {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		try {
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			String content = request.getParameter("content");
			PersistenceManager pm = PMF.get().getPersistenceManager();
			if (content != null) {
				GuestBook guestBook = new GuestBook(user, new Text(content), new Date());
				pm.makePersistent(guestBook);
			}
			pm.close();

			response.sendRedirect("/guestbook/");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
